package com.example.administrator.myapplication.Model;

import com.example.administrator.myapplication.Bean.AjaxResult;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Http.RetrofitHelper;
import com.example.administrator.myapplication.Presenter.ILoginListener;
import com.example.administrator.myapplication.Presenter.ILogonListener;

import java.io.IOException;

import javax.security.auth.callback.Callback;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2019/10/6 0006.
 */

public class LogonModel implements ILogonModel{
    @Override
    public void logon(User user, final ILogonListener iLogonListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.logon(user).enqueue(new retrofit2.Callback<AjaxResult>() {
            @Override
            public void onResponse(Call<AjaxResult> call, Response<AjaxResult> response) {
                iLogonListener.success(response.body());
            }

            @Override
            public void onFailure(Call<AjaxResult> call, Throwable t) {
                iLogonListener.fail(t.toString());
            }
        });
    }

    @Override
    public void checkLoginNameUnique(String loginName,final ILogonListener iLogonListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.checkLoginNameUnique(loginName).enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    iLogonListener.LoginNameUnique(new String(response.body().bytes()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                iLogonListener.fail(t.toString());
            }
        });
    }

    @Override
    public void checkPhoneUnique(String phonenumber,final ILogonListener iLogonListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.checkPhoneUnique(phonenumber).enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    iLogonListener.PhoneUnique(new String(response.body().bytes()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                iLogonListener.fail(t.toString());
            }
        });
    }


    @Override
    public void checkEmailUnique(String email,final ILogonListener iLogonListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.checkEmailUnique(email).enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    iLogonListener.EmailUnique(new String(response.body().bytes()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                iLogonListener.fail(t.toString());
            }
        });
    }
}
